package com.lxw.station.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lxw.station.core.R;
import com.lxw.station.core.Result;
import com.lxw.station.entity.Downloads;
import com.lxw.station.service.DownloadsService;
import lombok.SneakyThrows;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

@RestController
@RequestMapping("/downloads")
public class DownloadsController {

    @Resource
    DownloadsService downloadsService;


    @SneakyThrows
    @PostMapping("list")
    public Result<PageInfo<Downloads>> list(@RequestBody Downloads downloads) {
        PageHelper.startPage(downloads.getPageNum(), downloads.getPageSize());
        LambdaQueryWrapper<Downloads> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Downloads::getType, downloads.getType())
                .eq(StringUtils.hasText(downloads.getDate()), Downloads::getDate, downloads.getDate())
                .like(StringUtils.hasText(downloads.getTitle()), Downloads::getTitle, downloads.getTitle());
        String sortInfo = downloads.getSortInfo();
        if (StringUtils.hasText(sortInfo)) {
            String[] s = sortInfo.split(" ");
            if ("ascending".equals(s[1])) {
                wrapper.last( "order by "+s[0]+" asc");
            }else {
                wrapper.last( "order by "+s[0]+" desc");
            }
        }
        List<Downloads> list = downloadsService.list(wrapper);
        return R.ok(new PageInfo<>(list));
    }

    /**
     * 根据属性名动态获取实体字段值
     * @param entity    实体对象
     * @param fieldName 属性名
     * @return 属性值
     */
    private Object getFieldValue(Downloads entity, String fieldName) {
        // 反射获取字段值
        try {
            return entity.getClass().getMethod("get" + capitalize(fieldName)).invoke(entity);
        } catch (Exception e) {
            // 处理异常情况
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 将字符串首字母大写
     * @param str 字符串
     * @return 首字母大写后的字符串
     */
    private String capitalize(String str) {
        if (str == null || str.isEmpty()) {
            return str;
        }
        return Character.toUpperCase(str.charAt(0)) + str.substring(1);
    }
    // 根据字段名构建 lambda 表达式
    private  Function<Downloads, ?> getFieldFunction(String fieldName) {
        // 获取对应属性的 getter 方法名
        String getterMethodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);

        // 构建 Function，使用反射调用 getter 方法获取属性值
        return entity -> {
            try {
                Method getterMethod = Downloads.class.getMethod("getUsername");
                return getterMethod.invoke(entity);
            } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                e.printStackTrace();
                return null;
            }
        };
    }

    public static void main(String[] args) {


    }


}
